<script setup lang="ts">
import { changeStatus, deleteApi, getMyApiList, MyApiListItem, revokeApply } from '@/api/llm/model'
import { useRenderIcon } from '@/components/ReIcon/src/hooks'
import { usePagination } from '@/hooks/usePagination'
import { useTableHeight } from '@/hooks/useTableHeight'
import useTableRowSelection from '@/hooks/useTableRowSelection'
import useDictStore from '@/store/modules/dict'
import { ModalShowType } from '@/types/common'
import { formatDate, omitEmptyValue } from '@/utils/common'
import { message } from '@/utils/message'
import Refresh from '@iconify-icons/ep/refresh'
import Search from '@iconify-icons/ep/search'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import Edit from './edit.vue'

defineOptions({
  name: 'api-manage/list',
})

const { model_type } = useDictStore().getDicts('model_type')
const router = useRouter()
const loading = ref(false)
const queryParams = reactive({
  keyword: '',
})

const { pagination, paginationParams, changePageNumber, changePageSize } = usePagination(onSearch)
const { tableHeight, tableRef } = useTableHeight()
const columns: TableColumnList = [
  // {
  //   type: 'selection',
  //   width: 55,
  //   align: 'left',
  // },
  {
    label: '模型名称',
    prop: 'modelName',
    minWidth: 180,
  },
  {
    label: '模型类别',
    prop: 'modelType',
    minWidth: 150,
    formatter: ({ modelType }) => model_type.getLabel(modelType),
  },
  {
    label: '模型地址',
    prop: 'modelUrl',
    minWidth: 420,
  },
  {
    label: '模型密钥',
    prop: 'modelKey',
    // minWidth: 180,
    formatter({ modelKey }) {
      return modelKey || '/'
    },
  },
  {
    label: '操作',
    fixed: 'right',
    width: 180,
    slot: 'operation',
  },
]
const dataList = ref<Array<MyApiListItem>>([])

/** 查询列表 */
async function onSearch() {
  loading.value = true
  const { rows, total } = await getMyApiList(
    omitEmptyValue({
      keyword: queryParams.keyword,
      ...paginationParams.value,
    })
  ).finally(() => (loading.value = false))
  console.log({ rows })

  dataList.value = rows
  pagination.total = total
}

/** 重置新增的表单  */
async function resetForm() {
  queryParams.keyword = ''
  await onSearch()
}

const { selectedRows, onRowSelectionChange } = useTableRowSelection()

function onCreateApi() {
  router.push('/llm/model/create')
}

async function onDeleteAPi(id: string) {
  loading.value = true
  await deleteApi(id).finally(() => (loading.value = false))
  message('删除成功', { type: 'success' })
  onSearch()
}

const props = reactive({
  modelId: undefined,
  formData: {
    modelType: undefined,
    modelName: undefined,
    modelUrl: undefined,
    modelKey: undefined,
  },
  // modelType: undefined,
  // modelName: undefined,
  // modelUrl: undefined,
  // modelKey: undefined,
  modalShow: ModalShowType.Hide,
})

function onEditApi(row) {
  props.modelId = row.modelId
  props.formData.modelType = row.modelType.toString()
  props.formData.modelName = row.modelName
  props.formData.modelUrl = row.modelUrl
  props.formData.modelKey = row.modelKey
  props.modalShow = ModalShowType.Edit
}

onSearch()
</script>

<template>
  <div class="main table-view-layout">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryParams"
      class="bg-el-bg w-[99/100] pl-8 pt-4"
    >
      <el-form-item prop="name">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入搜索关键词"
          clearable
          style="width: 240px"
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <PureTableBar title="接口列表">
      <template #buttons>
        <el-button type="primary" @click="onCreateApi">创建模型</el-button>
      </template>
      <template #table>
        <pure-table
          ref="tableRef"
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :data="dataList"
          :columns="columns"
          :pagination="pagination"
          :max-height="tableHeight"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)',
          }"
          @page-size-change="changePageSize"
          @page-current-change="changePageNumber"
          @selection-change="onRowSelectionChange"
        >
          <template #operation="{ row }">
            <!-- <el-button link class="reset-margin" type="primary" @click="
              router.push({
                path: '/llm/model/detail',
                query: { modelId: row.modelId },
              })
              ">
              模型详情
            </el-button> -->
            <el-button link type="primary" @click="onEditApi(row)"> 编辑 </el-button>
            <el-popconfirm title="是否确认删除?" @confirm="onDeleteAPi(row.modelId)">
              <template #reference>
                <el-button link type="danger"> 删除 </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <!-- <Edit v-model:modalShow="props.modalShow" v-model:modelId="props.modelId"
     v-model:modelName="props.modelName"
      v-model:modelKey="props.modelKey" v-model:modelType="props.modelType" v-model:modelUrl="props.modelUrl"
      :on-search="onSearch" /> -->
    <Edit
      v-model:modalShow="props.modalShow"
      v-model:formData="props.formData"
      :modelId="props.modelId"
      :on-search="onSearch"
    />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
